package com.lsj.gulimall.auth.controller;

import com.alibaba.fastjson.TypeReference;
import com.lsj.common.utils.R;
import com.lsj.gulimall.auth.feign.MemberFeignService;
import com.lsj.gulimall.auth.vo.MemberEntityVo;
import com.lsj.gulimall.auth.vo.UserLoginVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 梁胜君
 * @version 1.0
 * @create 2022/3/30 1:10
 */
@Controller
@RequestMapping("/user")
@Slf4j
public class LoginController {
    @Autowired
    MemberFeignService memberFeignService;

    @Autowired
    RestTemplate restTemplate;


    @PostMapping("/login")
    public String login(@Valid UserLoginVo vo, BindingResult result, RedirectAttributes redirectAttributes){
        if (result.hasErrors()) {
            List<FieldError> fieldErrors = result.getFieldErrors();
            Map<String, String> errors = fieldErrors.stream().collect(Collectors.toMap(fieldError -> {
                return fieldError.getField();
            }, fieldError -> {
                return fieldError.getDefaultMessage();
            }));
            redirectAttributes.addFlashAttribute("errors",errors);
            return "redirect:http://shengjunauth.com/login.html";
        }
        R r = memberFeignService.login(vo);
        if (r.getCode() == 0) {
            // 远程登录成功
            redirectAttributes.addFlashAttribute("member",r.getData("member",new TypeReference<MemberEntityVo>(){}));
            return "redirect:http://shengjunmall.com";
        }else{
            // 调用远程登录失败
            redirectAttributes.addFlashAttribute("errors",r.getData("msg",new TypeReference<String>(){}));
            log.info("登录失败返回错误信息" + r.getData("msg",new TypeReference<String>(){}));
            return "redirect:http://shengjunauth.com/login.html";
        }
    }


    @RequestMapping("/githubLogin")
    public void githubLogin(HttpServletResponse response) throws IOException {
        //restTemplate.getForObject("",String.class,client_id)
        Map<String, Object> map = new HashMap<>();
        String url = "https://github.com/login/oauth/authorize?"+"client_id={client_id}"+"&"+"redirect_uri={redirect_uri}";
        map.put("client_id","c8d2bd63e3ecba1bb1f0");
        map.put("redirect_uri","http://shengjunauth.com/oauth2.0/github/success");
        ResponseEntity<String> responseEntity = restTemplate.getForEntity("https://github.com/login/oauth/authorize?client_id={client_id}&login=login&redirect_uri={redirect_uri}", String.class, map);
        /*String body = responseEntity.getBody();
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter writer = response.getWriter();
        writer.write(body);*/

    }
}


